﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WCFServiceWebRole1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public IEnumerable<MatHang> GetAllData()
        {
            using (var data = new DataClasses1DataContext())
            {
                var result = data.MatHangs.ToList();
                return result;
            }
        }

        public bool delete(string mamh)
        {
            using (var data = new DataClasses1DataContext())
            {
                try
                {
                    var re = (from inf in data.MatHangs where inf.Mahh == int.Parse(mamh) select inf).Single();

                    if (re == null) return false;
                    data.MatHangs.DeleteOnSubmit(re);
                    data.SubmitChanges();
                    return true;
                }
                catch (Exception)
                { return false; }
            }
        }

        public bool update(MatHang ifodata)
        {
            try
            {
                using (var data = new DataClasses1DataContext())
                {
                    var re = (from inf in data.MatHangs where inf.Mahh == ifodata.Mahh select inf).Single();

                    if (re == null) return false;
                    re.Tenmh = ifodata.Tenmh;
                    re.Dongia = ifodata.Dongia;
                    re.Ngayban = ifodata.Ngayban;

                    data.SubmitChanges();
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool insert(ref MatHang ifodata)
        {
            using (var data = new DataClasses1DataContext())
            
            {

                try
                {
                    MatHang ifo = new MatHang();

                    ifo.Tenmh = ifodata.Tenmh;
                    ifo.Dongia = ifodata.Dongia;
                    ifo.Ngayban = ifodata.Ngayban;


                    data.MatHangs.InsertOnSubmit(ifo);
                    data.SubmitChanges();
                    ifodata.Mahh = ifo.Mahh;
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }
    }
}
